// packageF/others/customerCenter/customerCenterTrack/customerCenterTrack.js
const skinBehavior = require('../../../../utils/themeSkin/skinBehavior');
import util from "../../../../utils/util";
const app = getApp();
Page({
  behaviors: [skinBehavior],
  /**
   * 页面的初始数据
   */
  data: {
    isShowCalendar: false,
    currentTime: '',
    timeText: "",

    markDate: [],

    resData: null,
    cumulative: {},
    history: [],
    port: [],
    rank: [],
    member: [],

    tabInd: 1,
    startTime:'',
    endtime:'',

    //more
    isLoadMore: true,
    total_page: 0,
    page: 1
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    if (options.is_type) {
      this.setData({
        is_type: options.is_type
      })
    }
    this.setData({
      startTime: util.formatTimes(new Date()),
      timeText: util.formatTimes(new Date()),
    })
    this.setData({
      endtime:`${Number(this.data.startTime.slice(0,4)) - 10}${this.data.startTime.slice(4)}`
    })
    console.log(this.data.endtime,'dddddddd')
    this.getData();
    this.getList();
  },
  selectTab(e) {
    let ind = e.currentTarget.dataset.key;
    this.setData({
      tabInd: ind
    })
    this.init();
    this.getList();
  },
  confirm(e){
    this.setData({
      timeText : e.detail.value
    })
    this.init();
    this.getData();
    this.getList();
  },
  init() {
    this.isLoadMore = true;
    this.setData({
      top_list:[],
      page:1,
      total_page:0
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    if (this.data.isLoadMore) {
      this.getMoreData();
    } else {
      console.log('没有更多了');
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  getData() {
    let that = this;
    let json = {};
    json.level_type = this.data.is_type;
    json.date = this.data.timeText;
    let urlStr = app.getNetAddresss("plugin.customer-center.frontend.controllers.browse.index");
    app._getNetWork({
      url: urlStr,
      data: json,
      success: (resdata) => {
        var res = resdata.data;
        if (res.result == 1) {
          that.setData({
            history: res.data.history_data,
            port: res.data.port_data,
            visit_data: res.data.visit_data,
            display_set: res.data.display_set
          });
          wx.setNavigationBarColor({
            frontColor: '#ffffff',
            backgroundColor: that.data.themeBaseColor,
            animation: {
              duration: 400,
              timingFunc: 'easeIn'
            }
          });
          wx.setNavigationBarTitle({
            title: '轨迹追踪',
          })
        } else {
          wx.showToast({
            icon: 'none',
            title: res.msg,
            duration: 1500
          });
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
  getList() {
    let json = {};
    json.level_type = this.data.is_type;
    json.date = this.data.timeText;
    json.top_type = this.data.tabInd;
    let urlStr = app.getNetAddresss("plugin.customer-center.frontend.controllers.browse.get-record-list");
    app._getNetWork({
      url: urlStr,
      data: json,
      success: (resdata) => {
        var res = resdata.data;
        if (res.result == 1) {
          this.data.isLoadMore = true;
          this.data.total_page = res.data.top_list.last_page;
          if (!this.data.total_page) {
            this.data.total_page = 0;
          }
          this.setData({
            top_list: res.data.top_list.data
          })
        } else {
          wx.showToast({
            icon: 'none',
            title: res.msg,
            duration: 1500
          });
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
  getMoreData() {
    let urlStr = "";
    this.data.isLoadMore = false; // 防止多次请求分页数据
    if (this.data.page >= this.data.total_page) {
      return;
    } else {
      let json = {};
      json.level_type = this.data.is_type;
      json.date = this.data.timeText;
      json.top_type = this.data.tabInd;
      this.data.page = this.data.page + 1;
      json.page = this.data.page;
      urlStr = app.getNetAddresss('plugin.customer-center.frontend.controllers.browse.get-record-list');
      app._postNetWork({
        url: urlStr,
        data: json,
        success: (resdata) => {
          let res = resdata.data
          if (res.result == 1) {
            this.data.isLoadMore = true;
            this.setData({
              top_list: this.data.top_list.concat(res.data.top_list.data)
            })
          } else {
            this.data.page = this.data.page - 1;
            this.isLoadMore = false
            wx.showToast({
              icon: 'none',
              title: res.msg,
              duration: 1500
            });
          }
        },
        fail: function (res) {
          console.log(res);
        }
      });
    }
  },
})